package org.example.task.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.task.model.dto.PageDTO;
import org.example.task.model.po.UserRole;
import org.example.task.mapper.UserRoleMapper;
import org.example.task.model.query.UserRolePageQuery;
import org.example.task.model.vo.userrole.UserRoleVO;
import org.example.task.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-06-30
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {
    @Override
    public PageDTO<UserRoleVO> getUserRolePage(UserRolePageQuery userRolePageQuery) {
        Page<UserRole> userRolePage = userRolePageQuery.toPage();
        lambdaQuery().eq(userRolePageQuery.getUserId() != null, UserRole::getUserId, userRolePageQuery.getUserId())
                .eq(userRolePageQuery.getRoleId() != null, UserRole::getRoleId, userRolePageQuery.getRoleId())
                .page(userRolePage);
        return PageDTO.of(userRolePage, UserRoleVO.class);
    }

    @Override
    public boolean removeUserRoleById(Long userId, Long roleId) {
        return lambdaUpdate().eq(UserRole::getUserId, userId).eq(UserRole::getRoleId, roleId).remove();
    }
}
